﻿using System.ComponentModel.Composition;
using System.Windows.Controls;
using MIXer.Contracts.ViewModels;
using MIXer.Core.Contracts;
using MIXer.Customization;

namespace MIXer.ViewModels
{
    [Export(typeof(IConfigViewModel))]
    public class ConfigViewModel : Tab, IConfigViewModel
    {
        #region Constructors
        public ConfigViewModel()
            :base("Configuration")
        {
        }
        #endregion

        #region Methods
        public void PasswordChanged(PasswordBox passwordControl)
        {
            ProxyPassword = passwordControl.Password;
        }
        #endregion

        #region Properties
        public bool UseProxy
        {
            get { return ConfigurationProvider.GetValueAs<bool>("UseProxy"); }
            set
            {
                ConfigurationProvider.SetValue("UseProxy", value);

                NotifyOfPropertyChange(() => UseProxy);
            }
        }
        public bool UseDefaultProxy
        {
            get { return ConfigurationProvider.GetValueAs<bool>("UseDefaultProxy"); }
            set
            {
                ConfigurationProvider.SetValue("UseDefaultProxy", value);

                NotifyOfPropertyChange(() => UseDefaultProxy);
            }
        }
        public string ProxyServer
        {
            get { return ConfigurationProvider.GetValueAs<string>("ProxyServer"); }
            set
            {
                ConfigurationProvider.SetValue("ProxyServer", value);

                NotifyOfPropertyChange(() => ProxyServer);
            }
        }
        public int ProxyPort
        {
            get { return ConfigurationProvider.GetValueAs<int>("ProxyPort"); }
            set
            {
                ConfigurationProvider.SetValue("ProxyPort", value);

                NotifyOfPropertyChange(() => ProxyPort);
            }
        }
        public string ProxyUser
        {
            get { return ConfigurationProvider.GetValueAs<string>("ProxyUser"); }
            set
            {
                ConfigurationProvider.SetValue("ProxyUser", value);

                NotifyOfPropertyChange(() => ProxyUser);
            }
        }
        public string ProxyPassword
        {
            get { return PasswordHelper.Decrypt(ConfigurationProvider.GetValueAs<string>("ProxyPassword")); }
            set
            {
                ConfigurationProvider.SetValue("ProxyPassword", PasswordHelper.Encrypt(value));

                NotifyOfPropertyChange(() => ProxyPassword);
            }
        }

        #region Providers
        [Import(typeof(IConfigurationProvider), AllowDefault = true)]
        public IConfigurationProvider ConfigurationProvider { get; set; }
        #endregion

        #endregion
    }
}
